import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute } from "@angular/router"
import { HttpClient } from "@angular/common/http"

@Component({
  selector: 'app-comtact-edit',
  templateUrl: './comtact-edit.component.html',
  styleUrls: ['./comtact-edit.component.css']
})
export class ComtactEditComponent implements OnInit {

  fromData:any={
    id: null,
    name: '',
    email: '',
    phone: ''
  }

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private http: HttpClient
  ) { }

  ngOnInit() {
    //获取动态参数id
    const contactId = this.route.snapshot.params.id
    this.http.get(`http://localhost:3000/contacts/${contactId}`)
    .toPromise()
    .then(data=>{
      console.log(data)
      this.fromData = data
    }).catch(err=>{
      console.log(err)
    })
  }

  updataContact(){
    const id = this.fromData.id
    this.http.patch(`http://localhost:3000/contacts/${id}`,this.fromData)
    .toPromise()
    .then(data=>{
      console.log(data)
      this.router.navigate(["/comtact"])  //跳转到联系人列表
      
    }).catch(err=>{
      console.log(err)
    })
  }

}
